
//
//  HMComposeToolbar.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/13.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HMComposeToolbar.h"

@interface HMComposeToolbar()

@property (nonatomic, strong) UIButton *emotionButton;

@end


@implementation HMComposeToolbar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //设置toolbar属性
        [self setupToolbar];
        
        //添加子控件
        [self addChildControl];
        
    }
    return self;
}

//设置toolbar属性
- (void)setupToolbar{
    self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_toolbar_background"]];

}

//添加子控件
- (void)addChildControl{

    //相机
    [self addButtonWithIcon:@"compose_camerabutton_background" heightIcon:@"compose_camerabutton_background_highlighted" tag:HMComposeToolbarButtonTypeCamera];
    
    //图片
    [self addButtonWithIcon:@"compose_toolbar_picture" heightIcon:@"compose_toolbar_picture_highlighted" tag:HMComposeToolbarButtonTypePicture];
    
    //@
    [self addButtonWithIcon:@"compose_mentionbutton_background" heightIcon:@"compose_mentionbutton_background_highlighted" tag:HMComposeToolbarButtonTypeMention];
    //话题
    [self addButtonWithIcon:@"compose_trendbutton_background" heightIcon:@"compose_trendbutton_background_highlighted" tag:HMComposeToolbarButtonTypeTrend];
    //表情
    self.emotionButton = [self addButtonWithIcon:@"compose_emoticonbutton_background" heightIcon:@"compose_emoticonbutton_background_highlighted" tag:HMComposeToolbarButtonTypeEmotion];
}

- (UIButton *)addButtonWithIcon:(NSString *)icon heightIcon:(NSString *)heightIcon tag:(HMComposeToolbarButtonType)tag{
    UIButton *button = [[UIButton alloc]init];
    button.tag = tag;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
    [button setImage:[UIImage imageWithName:heightIcon] forState:UIControlStateHighlighted];
    [self addSubview:button];
    
    return button;
}

//监听按钮点击
- (void)buttonClick:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(composeTool:didClickedButton:)]) {
        [self.delegate composeTool:self didClickedButton:(int)button.tag];
    }

}

//布局
- (void)layoutSubviews{
    [super layoutSubviews];
    int count = (int)self.subviews.count;
    CGFloat buttonW = self.width / count;
    CGFloat buttonH = self.height;
    for (int i = 0; i < count; i ++) {
        UIButton *button = self.subviews[i];
        button.y = 0;
        button.width = buttonW;
        button.height = buttonH;
        button.x = i * buttonW;
        
    }
}

//监听键盘显示 表情图标 还是 键盘图标
//根据传进来的内容,调用isShowEmotionButton 的setter 方法
//重写 isShowEmotionButton 的setter 方法
- (void)setShowEmotionButton:(BOOL)showEmotionButton{
    _showEmotionButton = showEmotionButton;
    
    if (showEmotionButton) {
        //如果 == YES,显示表情图标
        [self.emotionButton setImage:[UIImage imageWithName:@"compose_emoticonbutton_background"] forState:UIControlStateNormal];
        [self.emotionButton setImage:[UIImage imageWithName:@"compose_emoticonbutton_background_highlighted"] forState:UIControlStateHighlighted];
        
    }else{//显示 键盘图标
        [self.emotionButton setImage:[UIImage imageWithName:@"compose_keyboardbutton_background"] forState:UIControlStateNormal];
        [self.emotionButton setImage:[UIImage imageWithName:@"compose_keyboardbutton_background_highlighted"] forState:UIControlStateHighlighted];
    
    }

}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
